package top.lyjwn.ajui.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import top.lyjwn.ajui.activity.ChatDialogActivity;
import top.lyjwn.ajui.data.Message;
import top.lyjwn.ajui.util.LogUtil;

public class ChatDialogReceiver extends BroadcastReceiver {

    private ChatDialogActivity chatDialogActivity;

    public ChatDialogReceiver(ChatDialogActivity chatDialogActivity) {
        this.chatDialogActivity = chatDialogActivity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("top.lyjwn.ajui.receiver.ChatDialogReceiver".equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            String data = bundle.getString("data");
            JSONObject jsonObject = JSONObject.parseObject(data);
            LogUtil.d("聊天窗口接收器", "data: " + jsonObject.toJSONString());
            if (bundle != null) {
//                switch (jsonObject.getString("type")) {
//                    case "local_connect_status":
//                        break;
//                    case "chatMessage":
//                        String dataText = jsonObject.getString("dataText");
//                        break;
//                }
                Message message = JSONObject.parseObject(data, Message.class);
                if (message.getType()==0 || message.getType()==1){
                    chatDialogActivity.receiverMessage(message);
                }
            }
        }
    }
    public static void main(String[] args) {
        String s = "{\"content\":{\"text\":\"11\",\"type\":0},\"messageClientId\":\"30fa1783816c4c188f455a1d9bb840e4\",\"read\":false,\"receiver\":{\"contactAddress\":\"广西临桂县金水路中都市心语15栋一单元601\",\"contactTel\":\"18377315859\",\"email\":\"1606426239@qq.com\",\"id\":\"2020042110213132dc09640abc481188dacf55ca6b6849\",\"name\":\"罗渝杰\",\"sex\":0},\"sender\":{\"contactAddress\":\"广西临桂县金水路中都市心语15栋一单元601\",\"contactTel\":\"18377315859\",\"email\":\"1606426239@qq.com\",\"id\":\"2020042110213132dc09640abc481188dacf55ca6b6849\",\"name\":\"罗渝杰\",\"sendTime\":1590723187226,\"sex\":0}}\n";
        Message message = JSONObject.parseObject(s, Message.class);
    }
}
